package com.weike.lib.xxx;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;


public class MultiPagePrint {
	//多页打印技术
	static class PrintPage implements Printable {
		public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
				throws PrinterException {
			int x = (int) pageFormat.getImageableX();		// 获取可打印区域坐标的X位置
			int y = (int) pageFormat.getImageableY();		// 获取可打印区域坐标的Y位置
			Graphics2D g2 = (Graphics2D) graphics;
			g2.drawString("多页打印实例的第" + pageIndex + "页", x, y + 10); // 绘制文本
			return Printable.PAGE_EXISTS;					// 返回PAGE_EXISTS
		}
	}

	public static void main(String[] args) {
		PrinterJob job = PrinterJob.getPrinterJob();//单例模式必须通过PrinterJob.getPrinterJob()获取唯一实例
		if(!job.printDialog()){ // 如果取消打印对话框
			return ;
		}
		Book book = new Book();//多页打印类
		PageFormat pageFormat = new PageFormat();//打印页面格式类  颜色 方向控制
		book.append(new PrintPage(), pageFormat);// 添加1页
		book.append(new PrintPage(), pageFormat, 3);// 添加3页
		job.setPageable(book); //设置打印页内容
		job.setJobName("多页打印");//设置打印任务名称
		try {
			job.print();
			System.err.println("打印111111");
		} catch (PrinterException e) {
			System.err.println("打印出错！！！");
		}
	}
}
